package com.vjwealth.android.nos.activity.main

import android.content.Intent
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.support.v4.content.ContextCompat
import android.support.v4.view.ViewPager
import android.text.TextUtils
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.android.volley.Request
import com.android.volley.Response
import com.bumptech.glide.Glide
import com.google.gson.Gson
import com.vjwealth.android.nos.Constants

import com.vjwealth.android.nos.R
import com.vjwealth.android.nos.activity.rescue_list.RescueListActivity
import com.vjwealth.android.nos.adapter.ViewPagerAdapter
import com.vjwealth.android.nos.base.BaseActivity
import com.vjwealth.android.nos.base.BaseApplication
import com.vjwealth.android.nos.entity.CustomerInforData
import com.vjwealth.android.nos.entity.CustomerInforEntity
import com.vjwealth.android.nos.entity.HomeCarData
import com.vjwealth.android.nos.entity.HomeCarEntity
import com.vjwealth.android.nos.http.Api
import com.vjwealth.android.nos.http.BaseStringRequest
import com.vjwealth.android.nos.http.VolleyUtils
import com.vjwealth.android.nos.utils.ParamsUtils
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.layout_car_viewpager_item.*
import kotlinx.android.synthetic.main.layout_car_viewpager_item.view.*

class MainActivity : BaseActivity() {

    var lastPosition = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mShowTitleLayout = false
        setContentView(R.layout.activity_main)
        init()
    }

    private fun init(){
        mCircleMenuView.setMovieResource(R.raw.start_notifylight)
        mCircleMenuView.setOnCircleMenuClickListener { position: Int ->
            when (position){
                0 -> toModeActivity()
                1 -> toCatalogActivity()
                2 -> toSubscribeActivity()
                3 -> toMyNosActivity()
                4 -> toRescueActivity()
            }
        }

        fetchMyCarList()
        fetchCustomerInfor()
    }
    /**界面逻辑****************************************************************************************/
    //进入模式
    private fun toModeActivity(){    }

    //进入爱车履历
    private fun toCatalogActivity(){}

    //进入预约
    private fun toSubscribeActivity(){}

    //进入我的NOS
    private fun toMyNosActivity(){}

    //进入救援
    private fun toRescueActivity(){
        var intent = Intent(this@MainActivity, RescueListActivity::class.java)
        startActivity(intent)
    }

    //进入添加爱车
    private fun toCarAddActivity(){}

    //进入爱车详情
    private fun toCarDetailActivity(){}

    //爱车pager
    private fun showHomeCarViewPager(list: List<HomeCarData>){
        //处理viewpager下的指示点
        var size = if(list!!.size==0) 1 else list!!.size+1
        mCarViewPagerPointLayout.removeAllViews()
        var index = 0
        do {
            var pointView = ImageView(this@MainActivity)
            var layoutParams = LinearLayout.LayoutParams(10, 10)
            layoutParams.setMargins(5, 10, 5, 10)
            pointView.layoutParams = layoutParams
            if(index == 0){
                pointView.setBackgroundResource(R.drawable.ovl_focused_bg)
            }else{
                pointView.setBackgroundResource(R.drawable.ovl_normal_bg)
            }
            mCarViewPagerPointLayout.addView(pointView)
            index++
        }while (index < size)
        mCarViewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {}
            override fun onPageSelected(position: Int) {
                mCarViewPagerPointLayout.getChildAt(lastPosition).setBackgroundResource(R.drawable.ovl_normal_bg)
                lastPosition = position
                mCarViewPagerPointLayout.getChildAt(position).setBackgroundResource(R.drawable.ovl_focused_bg)
            }
            override fun onPageScrollStateChanged(state: Int) {}
        })
        //处理viewpager
        var dataList: MutableList<HomeCarData> = ArrayList()
        dataList.addAll(list)
        dataList.add(HomeCarData())
        val viewPagerAdapter = object : ViewPagerAdapter<HomeCarData>(this@MainActivity, R.layout.layout_car_viewpager_item, dataList) {
            override fun convert(view: View, position: Int, homeCarData: HomeCarData) {
                if(homeCarData.cusCarId == 0){
                    view!!.mViewPagerItemImage.setImageResource(R.mipmap.my_car_add)
                    view!!.mViewPagerItemName.text = ""
                    view!!.setOnClickListener { view ->
                        toCarAddActivity()
                    }
                }else{
                    Glide.with(this@MainActivity).load(Api.BASE_IMAGE_URL + homeCarData.image).placeholder(R.mipmap.home_dispaly_bg).into(view!!.mViewPagerItemImage)
                    view!!.mViewPagerItemName.text = homeCarData.carTypeName
                    view!!.setOnClickListener { view ->
                        toCarDetailActivity()
                    }
                }
            }
        }
        mCarViewPager.adapter = viewPagerAdapter
    }

    //用户信息
    private fun showCustomerInfor(infor: CustomerInforData){
        if(TextUtils.isEmpty(infor.avatar)){
            if("1" == infor.gender) {
                Glide.with(this@MainActivity).load(Api.BASE_IMAGE_URL + infor.avatar)
                        .placeholder(ContextCompat.getDrawable(this, R.mipmap.man_avator)).into(mUserIconImage)
            } else {
                Glide.with(this@MainActivity).load(Api.BASE_IMAGE_URL + infor.avatar)
                        .placeholder(ContextCompat.getDrawable(this, R.mipmap.uer_icon)).into(mUserIconImage)
            }
        }else{
            Glide.with(this@MainActivity).load(Api.BASE_IMAGE_URL + infor.avatar).into(mUserIconImage)
        }
        showLeftMenuView()
    }

    //显示侧边导航
    private fun showLeftMenuView(){
        val fm: FragmentManager = supportFragmentManager
        //添加菜单
        fm.beginTransaction().add(R.id.id_left_menu, MenuLeftFragment()).commit()

    }
    /**接口调用*************************************************************/
    //查询我的爱车列表
    private fun fetchMyCarList(){
        var paramsMap = hashMapOf<String, String>()
        paramsMap.put("customerId", BaseApplication.spInfor!!.getString(Constants.LOGIN_USER_ID))
        var request = BaseStringRequest(Request.Method.GET, Api.car_my_list+ParamsUtils.g_map2string(paramsMap),
                Response.Listener { response ->
                    var homeCarEntity = Gson().fromJson(response.toString(), HomeCarEntity::class.java)
                    if(homeCarEntity.meta.success){
                        BaseApplication.spInfor!!.putHomeCarList(homeCarEntity.data)
                        showHomeCarViewPager(homeCarEntity.data)
                    }else{
                        showExceptionMessage(homeCarEntity.meta.message)
                    }
                },
                Response.ErrorListener {

                })
        VolleyUtils.requestQueue.add(request)
        VolleyUtils.requestQueue.start()
    }
    //查询用户数据
    private fun fetchCustomerInfor(){
        var customerId = BaseApplication.spInfor!!.getString(Constants.LOGIN_USER_ID)
        if(!TextUtils.isEmpty(customerId)){
            var map = hashMapOf("customerId" to customerId)
            var request = BaseStringRequest(Request.Method.GET, Api.customer_init+ParamsUtils.g_map2string(map),
                    Response.Listener { response ->
                        var customerInfor = Gson().fromJson(response.toString(), CustomerInforEntity::class.java)
                        if(customerInfor.meta.success) {
                            BaseApplication.spInfor!!.putInitDataByShared(customerInfor.data)
                            showCustomerInfor(customerInfor.data)
                        }else{
                            showExceptionMessage(customerInfor.meta.message)
                        }
                    },
                    Response.ErrorListener {  })
            VolleyUtils.requestQueue.add(request)
            VolleyUtils.requestQueue.start()
        }
    }
}
